/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.backend.dao.impl;

import java.math.BigDecimal;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pneuservis.backend.dao.ServicePneuDAOLocal;
import pneuservis.backend.entities.ServicePneu;

/**
 *
 * @author Admin
 */
@Stateless
public class ServicePneuDAO implements ServicePneuDAOLocal {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public void createService(ServicePneu servicePneu) throws IllegalArgumentException{
        validate(servicePneu); 
        entityManager.persist(servicePneu);
    }

    @Override
    public ServicePneu getService(Long id) throws IllegalArgumentException{
        if (id == null) {
            throw new IllegalArgumentException("ID is null.");
        }
        ServicePneu servicePneu = entityManager.find(ServicePneu.class, id);
        if(servicePneu == null){
            return null;
        }
        entityManager.detach(servicePneu);
        return servicePneu;
    }

    @Override
    public void updateService(ServicePneu servicePneu) throws IllegalArgumentException{
        if (servicePneu == null) {
            throw new IllegalArgumentException("Service can't be null.");
        }
        if (servicePneu.getId() == null) {
            throw new IllegalArgumentException("You can't update service with null ID");
        }
        validate(servicePneu);
        ServicePneu result = entityManager.find(ServicePneu.class, servicePneu.getId());
        if (result == null) {
            throw new IllegalArgumentException("Service {0} is not in database." + servicePneu);
        } 
        entityManager.merge(servicePneu);
    }

    @Override
    public void removeService(ServicePneu servicePneu) throws IllegalArgumentException{
        if (servicePneu == null) {
            throw new IllegalArgumentException("Service can't be null.");
        }
        if (servicePneu.getId() == null) {
            throw new IllegalArgumentException("You can't remove service with null ID");
        }
        ServicePneu result = entityManager.find(ServicePneu.class, servicePneu.getId());
        if (result == null) {
            throw new IllegalArgumentException("Service {0} is not in database." + servicePneu);
        }
        entityManager.remove(result);
    }

    @Override
    public List<ServicePneu> getAllServices() {
        
        List<ServicePneu> resultList;
        Query query = entityManager.createQuery("SELECT s FROM ServicePneu s ORDER BY s.id");
        
        resultList = query.getResultList();
        for(ServicePneu s : resultList){
            entityManager.detach(s);
        }
        return resultList;
    }

    @Override
    public List<ServicePneu> findServiceWithName(String name) throws IllegalArgumentException{
        if (name == null) {
            throw new IllegalArgumentException("You can't find service with null name");
        }
        List<ServicePneu> resultList;
            Query query = entityManager.createQuery("SELECT s FROM ServicePneu s WHERE s.name LIKE :name ORDER BY s.id");
        query.setParameter("name", name);
        
        resultList = query.getResultList();
        for(ServicePneu s : resultList){
            entityManager.detach(s);
        }
        return resultList;
    }
    
    private void validate(ServicePneu servicePneu) {
        if (servicePneu == null) {
            throw new IllegalArgumentException("service can't be null.");
        }
        if (servicePneu.getName() == null) {
            throw new IllegalArgumentException("Name can't be null.");
        }
        if (servicePneu.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("Name with wrong attribute.");
        }
        if (servicePneu.getPrice() == null) {
            throw new IllegalArgumentException("Price can't be null.");
        }
        if (servicePneu.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("Price can't be negative or 0.");
        }
    }

}
